var registered = false;

/** OBSOLOTE */
//call function by every window-resizing + ini
function magicLoginFrame_init(){
    //get x van 'login-li'
	var listitem_x = $("#menu li:first").offset().left;
 	$("#loginframe").css("left", listitem_x);
}

function initFbUser(){
	
	var user;
	
	if(registered)
		user = new FbUser(getCookie( "username"), getCookie( "accesstoken" ) );
	else
	{
		getPermission();
		if( registered ) 
			FB.getLoginStatus( getLoginStatus );
	}	
		
	return user;
	
}


//Not used yet
function getLoginStatus ( response ) {
		try{
			safelog(response);
			
			if (!response || response.error){
				safelog(response.error);
			}
			else if (response.status === 'connected') {
				safelog(response.status);
			
				var userID = response.authResponse.userID;
				var accessToken = response.authResponse.accessToken;
				
				
				
				storeCookies( userID, accessToken );
				
				//$('#loginmenu').click(logout);
				
				registered = true;
						
				
				
			} else if (response.status === 'not_authorized') {
			/*
			*	UNABLE CLICKING MOVIES
			*	ASK FOR AUTH
			*/
				safelog(response.status);
				
				registered = false;
			} else {
			/*
			*	UNABLE CLICKING MOVIES
			*	SHOW FRIENDS
			*/
				safelog(response.status);
				
				registered = false;
			}
		}
		catch(err){
			safelog(err);
		}
	
}

function checkStatusAtStart( response ) {
	try{
			safelog(response);
			
			if (!response || response.error){
				safelog(response.error);
			}
			else if (response.status === 'connected') {
				safelog(response.status);
			
				var userID = response.authResponse.userID;
				var accessToken = response.authResponse.accessToken;
				
				changeLoginSpanText("connected");
				afterLogin(); //fireworks-function
				//getfriends
				
				
				//$('#loginmenu').click(logout);
			
				
			} else if (response.status === 'not_authorized') {
			/*
			*	UNABLE CLICKING MOVIES
			*	ASK FOR AUTH
			*/
				safelog(response.status);
				changeLoginSpanText("login");
				
			//	$('#twofifty').click(askForLogin);
			
				
			} else {
			/*
			*	UNABLE CLICKING MOVIES
			*	SHOW FRIENDS
			*/
				safelog(response.status);
				changeLoginSpanText("login");
				
				//redirect: now logged in
			}
		}
		catch(err){
			safelog(err);
		}
}

function askForLogin (){
		alert("Please login first");
}

function fb_login(){
	FB.login( function( response ){
	
		if(response.authResponse){
			$('#twofifty').unbind('click');
		}
		else {
			//apologize	
		}
		
	});	
}

function storeCookies ( userID, accessToken ){
	
		setCookie("userID", userID, 30);
		setCookie("accessToken", accessToken, 30);
		
}

function setCookie (c_name, value, exdays){
		safelog("setting cookie");
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value = escape(value) + ((exdays === null) ? "" : ";expires = "+exdate.toUTCString());
		document.cookie = c_name+"="+ c_value;	
}

function getCookie ( c_name ) {
		
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++)
		{
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if( x==c_name )
			{
				return unescape(y);
			}
		}
		
}

function getPermission(){
	window.location.replace("https://www.facebook.com/dialog/oauth/?client_id=128851227267256&redirect_uri=http://soofie.be/jill/twofifty&state=chickensoup&display=popup&response_type=token");
	var accessToken = getURLParameter("access_token");
	var error = getURLParameter("error");
	
	if(error)
		registered = false;
	else
		registered = true;
		
//	window.location.replace("http://soofie.be/jill/twofifty");
	
}

function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}

function changeLoginSpanText(_text){
	$('#loginmenu').html(_text);	
}

function getMoviesForUser(){
	var request = $.ajax({
		type: "POST",
		url: "ajax/ajaxhandler.php",
		data: {method: "getMoviesFromUser", userid: _userid}
	});
	
	request.done(function(msg){
        $("log").html(msg);
    });

    request.fail(function(jqXHR, textStatus){
        alert("Request failed: " + textStatus);
    });
}

function getFriendsWithSeenMovies(_userid){
	var request = $.ajax({
		type: "POST",
		url: "ajax/ajaxhandler.php",
		data: {method: "getFriendsFromUser", userid: _userid}
	});
	
	request.done(function(msg){
        safelog(msg + " fireworks, 126");
    });

    request.fail(function(jqXHR, textStatus){
        alert("Request failed: " + textStatus);
    });	
}


